/**
 * 
 */
package com.zq.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.stream.IntStream;

/**
 * 说明:
 * 
 * @version v1.0
 * @author zhangqian(mailto:739428054@qq.com) 创建于:2020年4月24日上午11:33:04
 */
public class DateUtils {
	public enum Season {
		SPRING, SUMMER, AUTUMN, WINTER
	}

	/**
	 * 功能：根据生日求年龄 示例： 现在是2020-4-23，如果生日是2000-5-20，那结果是19
	 *
	 * @param date
	 * @return
	 */
	public static int getAge(Date birthday) {
		Calendar cal = Calendar.getInstance();
		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH) + 1;
		int dayNow = cal.get(Calendar.DAY_OF_MONTH);

		cal.setTime(birthday);
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH) + 1;
		int dayBirth = cal.get(Calendar.DAY_OF_MONTH);
		int age = yearNow - yearBirth;
		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				if (dayNow < dayBirth) {
					age--;
				}
			} else {
				age--;
			}
		}
		return age;
	}

	/**
	 * 获取当前季节
	 * 
	 * @return
	 */
	public static Season getCurrentSeason() {
		int month = Calendar.DAY_OF_MONTH + 1;
		if (month < 7) {
			if (month >= 1 && month <= 3) {
				return Season.SPRING;
			} else if (month >= 4 && month <= 6) {
				return Season.SUMMER;
			}
			if (month >= 7 && month <= 9) {
				return Season.AUTUMN;
			} else if (month >= 10 && month <= 12) {
				return Season.WINTER;
			}

		}
		return null;
	}
	
	/**
	 * 获取月初时间
	 * 功能：给定时间对象，获取该时间的月初1日0时0分0秒0毫秒
	 * 示例：2018-11-11 08:30:16 → 2018-11-01 00:00:00
	 * 使用场景：可用在数据库里查询某月的时间范围
	 */
	public static Date getFirstDayOfMonth(Date src){
		Calendar cal = Calendar.getInstance();
		cal.setTime(src);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}
	
	/**
	* 功能：给定时间对象，获取该时间的月末最后一天的23时23分59秒999毫秒
	* 示例：2018-11-11 08:30:16 → 2018-11-30 23:23:59
	* 使用场景：可用在数据库里查询某月的时间范围
	*
	* @param Date 源时间
	* @return Date 月末时间
	*/
	public static Date getLastDayOfMonth(Date src){
		Calendar cal = Calendar.getInstance();
		cal.setTime(src);
		cal.add(Calendar.MONTH,1);
		Date firstDayOfMonth = getFirstDayOfMonth(cal.getTime());
		cal.setTime(firstDayOfMonth);
		cal.add(Calendar.SECOND, -1);
		return cal.getTime();
	}
	
	/**
	* 功能：获取人性化时间，例如5分钟之内则显示“刚刚”，其它显示16分钟前、2小时前、3天前、4月前、5年前等
	*
	* @param Date date 源时间。
	* @return 人性化时间
	*/
	public static String getDisplayTime(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		//cal.add(Calendar.MONTH,-1);
		Calendar cal2 = Calendar.getInstance();
		long timeInMillis = cal.getTimeInMillis();
		long timeInMillis2 = cal2.getTimeInMillis();
		long time=(timeInMillis2-timeInMillis)/1000;
		if (time>0) {
			if (time>=1&&time<60) {
				long num=time;
				return num+"秒前";
			}else if (time>=60&&time<60*60) {
				long num=time/60;
				return num+"分钟前";
			}else if (time>=60*60&&time<60*60*24) {
				long num=time/60/60;
				return num+"小时前";
			}else if (time>=60*60*24&&time<60*60*24*30) {
				long num=time/60/60/24;
				return num+"天前";
			}else if (time>=60*60*24*30&&time<60*60*24*365) {
				long num=time/60/60/24/30;
				return num+"月前";
			}else if (time>=60*60*24*365) {
				long num=time/60/60/24/365;
				return num+"年前";
			}
			
			return "刚刚";
		}
		return "时间输入错误";
		
	}
	
	
	/**
	* 功能：获取当前日期指定天数之前或之后的日期，如果参数为负数，则往前回滚。
	* 示例1：当前是2018-08-08，参数days=5，结果是2018-08-13
	* 示例2：当前是2018-08-08，参数days=-4，结果是2018-08-04
	* 
	* @param int days 偏移的天数，如果为负数则往前回滚。
	* @return Date 新日期
	*/
	public static Date offsetDate(int days) {
		Calendar cal = Calendar.getInstance();
		int day = cal.get(Calendar.DAY_OF_MONTH);
			cal.set(Calendar.DAY_OF_MONTH, day+days);
			return cal.getTime();
		
	}
	
	/**
	 * 获取指定日期到现在的日期
	 */
	public static Date randomDate(String beginDate,String endDate){
	
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date start = format.parse(beginDate);
			Date end = format.parse(endDate);
			if (start.getTime()>=end.getTime()) {
				return null;
			}
			long date = random(start.getTime(), end.getTime());
			return new Date(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
			return null;
	}
	public static long random(long begin,long end){
		long rtn=begin+(long)(Math.random()*(end-begin));
		if (rtn==begin ||rtn==end) {
			return random(begin, end);
		}
		return rtn;
	}
	
}
